package com.yc.jpyy.base.controller;


import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.yc.jpyy.base.bean.RestResponse;


public abstract class BaseController{
	
	private static Logger logger = LoggerFactory.getLogger(BaseController.class);

	
	/**
	 * application/json
	 * @param body
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	protected Map<String, Object> getPostParams(String body) throws Exception{
		logger.debug("params:"+body);
		Map<String, Object> paramMap = JSON.parseObject(body, Map.class);
		return paramMap;
	}
	
	/**
	 * x-stroam/form 方式提交
	 * @param request
	 * @return
	 * @throws IOException
	 */
	protected Map<String, Object> getParamMap(HttpServletRequest request) throws IOException{
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		Map<String, String[]> parameterMap = (Map<String, String[]>)request.getParameterMap();
		for(String key : parameterMap.keySet()){
			String value = parameterMap.get(key)[0];
			if(value.equals("\"\"")){
				value="";
			}
			paramsMap.put(key, value);
		}
		return paramsMap;
	}

	/**
	 * @Title: errorHandler 
	 * @Description: TODO(错误处理) 
	 * @param @param e
	 * @param @param logger    设定文件 
	 * @return void    返回类型 
	 * @throws
	 */
	protected void errorHandler(Exception e, Logger logger){
		if(e instanceof java.net.SocketTimeoutException){
			logger.error("接口访问超时！",e);
		}else{
			logger.error("系统处理异常",e);
		}
	}

	
	/**
	 * @Title: errorHandler 
	 * @Description: TODO(错误处理) 
	 * @param @param e
	 * @param @param logger    设定文件 
	 * @return void    返回类型 
	 * @throws
	 */
	protected void errorHandler(Exception e, RestResponse response, Logger logger){
    	if(response == null){
    		response = new RestResponse();
    		response.setErrorcode(0);
    	}
	   	 if(e instanceof java.net.SocketTimeoutException){
	       	 response.setMessage("接口访问超时！");
	   	 }else{
	       	 response.setMessage("系统处理异常");
	   	 }
	   	logger.error(response.getMessage(),e);
   }
	
	/**
	 * @Description: 获取包括当天日期在内的七天日期 
	 * @return list 
	 */
	public List<LocalDateTime> getSevenbDay(){
		//DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		List<LocalDateTime> sevenDay = new ArrayList<>();
		LocalDateTime localDateTime = LocalDate.now().atStartOfDay();
		sevenDay.add(localDateTime);
		sevenDay.add(localDateTime.plusDays(1));
		sevenDay.add(localDateTime.plusDays(2));
		sevenDay.add(localDateTime.plusDays(3));
		sevenDay.add(localDateTime.plusDays(4));
		sevenDay.add(localDateTime.plusDays(5));
		sevenDay.add(localDateTime.plusDays(6));
		return sevenDay;
   }
	
	
	/**
	 * @Description: 获取星期几 
	 * @return list 
	 */
	public String getWeekDay(LocalDateTime time){
		String week = "";
		int value = time.getDayOfWeek().getValue();
		if(value == 1){
			week = "星期一";
		}else if(value == 2){
			week = "星期二";
		}else if(value == 3){
			week = "星期三";
		}else if(value == 4){
			week = "星期四";
		}else if(value == 5){
			week = "星期五";
		}else if(value == 6){
			week = "星期六";
		}else if(value == 7){
			week = "星期日";
		}
		return week;
   }



}
